/*
 * File.cpp
 *
 *  Created on: 2011. 3. 15.
 *      Author: JeeHoon
 */

#include <FSystem.h>
#include "plugin/File.h"
#include "api/PluginManager.h"
#include "api/PluginResult.h"

using namespace Osp::Base;

File::File()
{
}

File::~File()
{
}

result
File::OnConstruct(PluginManager * pPluginManager, Osp::Base::String pluginName)
{
	result r = E_SUCCESS;

	return r;
}

result
File::execute(Osp::Base::String action, Osp::Base::String args, Osp::Base::String callbackId)
{
	result r = E_SUCCESS;

	if(action == L"getFileBasePaths")
	{
		__pPluginManager->evaluateJavascript(String() + L"navigator.fileMgr._setPaths('" + APP_DOCS_PATH + L"','" + APP_TEMP_PATH + L"', '" + APP_LIBRARY_PATH + L"');");
	}
	else if(action == L"getFreeDiskSpace")
	{
		long long nAbailableSize;
		String abailableSize;

		Osp::System::RuntimeInfo::GetValue(L"AvailableInternalStorage",nAbailableSize );
		abailableSize = Osp::Base::Long::ToString(nAbailableSize);

		__pPluginManager->evaluateJavascript(L"navigator.file._setFreeDiskSpace("+ abailableSize +L"); navigator.file.successCallback("+ abailableSize +L");");
	}
	else if(action == L"readFile")
	{
		printLog(L"Unknown action: "+action);

	}
	else if(action == L"write")
	{
		int index;
		if( callbackId.IndexOf('/',0,index) == E_SUCCESS )
		{
			String filePath;
			String data;

			callbackId.SubString(index+1,data);
			callbackId.SubString(0,index,filePath);
			filePath = URLDecode(filePath);
			data = URLDecode(data);

			printLog(filePath);
			printLog(data);
		}
		else
		{
			// exception
		}
	}
	else
	{
		printLog(L"Unknown action: "+action);
	}

	return r;
}

